package com.hzy.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.pms.domain.bo.ProductSnapshotBo;
import com.hzy.pms.domain.vo.ProductSnapshotVo;
import com.hzy.pms.domain.ProductSnapshot;
import com.hzy.pms.mapper.ProductSnapshotMapper;
import com.hzy.pms.service.IProductSnapshotService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品信息Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class ProductSnapshotServiceImpl implements IProductSnapshotService {

    private final ProductSnapshotMapper baseMapper;

    /**
     * 查询商品信息
     */
    @Override
    public ProductSnapshotVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品信息列表
     */
    @Override
    public TableDataInfo<ProductSnapshotVo> queryPageList(ProductSnapshotBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductSnapshot> lqw = buildQueryWrapper(bo);
        Page<ProductSnapshotVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品信息列表
     */
    @Override
    public List<ProductSnapshotVo> queryList(ProductSnapshotBo bo) {
        LambdaQueryWrapper<ProductSnapshot> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductSnapshot> buildQueryWrapper(ProductSnapshotBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductSnapshot> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, ProductSnapshot::getProductId, bo.getProductId());
        lqw.eq(bo.getBrandId() != null, ProductSnapshot::getBrandId, bo.getBrandId());
        lqw.eq(bo.getCategoryId() != null, ProductSnapshot::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutProductId()), ProductSnapshot::getOutProductId, bo.getOutProductId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ProductSnapshot::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), ProductSnapshot::getPic, bo.getPic());
        lqw.eq(bo.getPublishStatus() != null, ProductSnapshot::getPublishStatus, bo.getPublishStatus());
        lqw.eq(bo.getSort() != null, ProductSnapshot::getSort, bo.getSort());
        lqw.eq(bo.getPrice() != null, ProductSnapshot::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), ProductSnapshot::getUnit, bo.getUnit());
        lqw.eq(bo.getWeight() != null, ProductSnapshot::getWeight, bo.getWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailHtml()), ProductSnapshot::getDetailHtml, bo.getDetailHtml());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailMobileHtml()), ProductSnapshot::getDetailMobileHtml, bo.getDetailMobileHtml());
        lqw.like(StringUtils.isNotBlank(bo.getBrandName()), ProductSnapshot::getBrandName, bo.getBrandName());
        lqw.like(StringUtils.isNotBlank(bo.getProductCategoryName()), ProductSnapshot::getProductCategoryName, bo.getProductCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductAttr()), ProductSnapshot::getProductAttr, bo.getProductAttr());
        return lqw;
    }

    /**
     * 新增商品信息
     */
    @Override
    public Boolean insertByBo(ProductSnapshotBo bo) {
        ProductSnapshot add = BeanUtil.toBean(bo, ProductSnapshot.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品信息
     */
    @Override
    public Boolean updateByBo(ProductSnapshotBo bo) {
        ProductSnapshot update = BeanUtil.toBean(bo, ProductSnapshot.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductSnapshot entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
